package com.zaofan.controller;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.Product;
import com.zaofan.bean.QualityExtend;
import com.zaofan.bean.ResultProduct;
import com.zaofan.bean.Shop;
import com.zaofan.service.ProductService;
import com.zaofan.service.ShopService;
import com.zaofan.utils.JPushUtils;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;
import org.slf4j.Logger;

@WebServlet("/product")
public class ProductController extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private ProductService productService = new ProductService();
	private ShopService shopService = new ShopService();

	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				params.put(name, value);
			}
			List<Product> products = productService.findAll(params);
			Long count = productService.count(params);
			JsonUtils.writeSuccessPageJson("查询成功", products, count, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Product product = WebUtils.write2bean(request, Product.class);
			QualityExtend qualityExtend =  WebUtils.write2bean(request, QualityExtend.class);
			String attr = request.getParameter("attr");
			productService.add(product, attr, qualityExtend);
			JsonUtils.writeSuccessJson("添加成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("添加失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("添加失败!!", response);
	}

	protected void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Product product = WebUtils.write2bean(request, Product.class);
			QualityExtend qualityExtend =  WebUtils.write2bean(request, QualityExtend.class);
			String attr = request.getParameter("attr");
			productService.update(product, attr, qualityExtend);
			JsonUtils.writeSuccessJson("修改成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("修改失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("修改失败!!", response);
	}

	protected void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			Integer id = Integer.valueOf(id_s);



			ResultProduct product = productService.findByIdAndAttr(id);

			JsonUtils.writeSuccessJson("查询成功", product, response);
			System.out.print(product);  //检查输出
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询成功!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询成功!!", response);
	}

	protected void state(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String temp = request.getParameter("id");
		try {
			Integer id = Integer.valueOf(temp);
			Product product = productService.findById(id);
			if (product == null) {
				JsonUtils.writeFailJson("参数错误!!", response);
				return;
			}
			product.setState(product.getState() == 0 ? 1 : 0);
			productService.setProductState(product);
			JsonUtils.writeSuccessJson("更新成功!!", "", response);
		} catch (Exception e) {
			JsonUtils.writeFailJson("参数错误!!", response);
		}
		JsonUtils.writeFailJson("更新失败!!", response);
	}

	protected void push(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String temp = request.getParameter("id");
		try {
			Integer id = Integer.valueOf(temp);
			Product product = productService.findById(id);
			if (product == null) {
				JsonUtils.writeFailJson("参数错误!!", response);
				return;
			}
			Shop shop = shopService.findById(product.getShopId());
			JPushUtils.sendProduct(product, shop);
			JsonUtils.writeSuccessJson("推送成功!!", "", response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("参数错误!!", response);
		}
		JsonUtils.writeFailJson("推送失败!!", response);
	}

}
